package com.example.chenduoyun.safe.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.telephony.SmsMessage;

import com.example.chenduoyun.safe.R;
import com.example.chenduoyun.safe.services.LocationService;
import com.example.chenduoyun.safe.utils.ConstantValue;
import com.example.chenduoyun.safe.utils.SpUtils;

/**
 * @author by created chen cloudy 2018/10/15 17:15
 **/

public class SmsReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        boolean open_security = SpUtils.getBoolean(context, ConstantValue.OPEN_SECURITY, false);
        if (open_security) {
            Object[] objects = (Object[]) intent.getExtras().get("pdus");
            for (Object object : objects) {
                SmsMessage sms = SmsMessage.createFromPdu((byte[]) object);
                String originatingAddress = sms.getOriginatingAddress();
                String messageBody = sms.getMessageBody();
                if(messageBody.contains("#*alarm*#")){
                    MediaPlayer player = MediaPlayer.create(context, R.raw.ylzs);
                    player.setLooping(true);
                    player.start();
                }
                if(messageBody.contains("#*location*#")){
                   context.startService(new Intent(context, LocationService.class));
                }
            }
        }
    }

}
